﻿using FluentMigrator;

namespace RockBus.Migrations.Version1_0
{
    [Migration(7)]
    public class M0007PipelineTables: Migration
    {
        public override void Down()
        {
            throw new System.NotImplementedException();
        }

        public override void Up()
        {
            Delete.ForeignKey("FK_MessageBuffer_Message_MessageBuffer").OnTable("MessageBuffer_Message");
            Delete.ForeignKey("FK_Subscription_DeliveryModeEnum").OnTable("Subscription");
            Delete.ForeignKey("FK_Subscription_Transf_Subscription").OnTable("Subscription_Transformation");
            Delete.ForeignKey("FK_Subscription_Transformation_Transf").OnTable("Subscription_Transformation");
            Delete.ForeignKey("FK_Transformation_Client").OnTable("Transformation");
            Delete.ForeignKey("FK_Transformation_TransfTypeEnum").OnTable("Transformation");
            Delete.ForeignKey("FK_Advertisement_Transf_Advertisement").OnTable("Advertisement_Transformation");
            Delete.ForeignKey("FK_Advertisement_Transformation_Transf").OnTable("Advertisement_Transformation");

            Delete.Table("MessageBuffer_Message");
            Delete.Table("MessageBuffer");
            Delete.Table("TransformationTypeEnum");
            Delete.Table("Transformation");
            Delete.Table("Advertisement_Transformation");
            Delete.Table("Subscription_Transformation");
            Delete.Table("DeliveryModeEnum");

            Delete.Column("DeliveryModeDbId").FromTable("Subscription");
            Delete.Column("GroupId").FromTable("Subscription");
            Alter.Table("Subscription")
                .AddColumn("RequestPipelineId").AsString(250).Nullable()
                .AddColumn("ResponsePipelineId").AsString(250).Nullable();

            Delete.Column("MessageBufferId").FromTable("Advertisement");
            Alter.Table("Advertisement")
                .AddColumn("RequestPipelineId").AsString(250).Nullable()
                .AddColumn("ResponsePipelineId").AsString(250).Nullable();

            //Create.ForeignKey("FK_Advertisement_Advertisement").FromTable("Advertisement").ForeignColumn("DbId")
            //    .ToTable("Advertisement").PrimaryColumn("DbId");

            Create.Table("Pipeline")
                .WithColumn("DbId").AsInt64().Identity().NotNullable()
                .WithColumn("Id").AsString(250).NotNullable()
                .WithColumn("ClientDbId").AsInt64().NotNullable()
                .WithColumn("PipelineInfo").AsXml().NotNullable();
            Create.PrimaryKey("PK_Pipeline").OnTable("Pipeline").Column("DbId");

            Create.ForeignKey("FK_Pipeline_Client")
                .FromTable("Pipeline").ForeignColumn("ClientDbId")
                .ToTable("Client").PrimaryColumn("DbId");
        }
    }
}
